Conditions | 10 |
Total Lines | 138 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like HillChart.renderGroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import EventEmitter from 'event-emitter-es6'; |
||
177 | |||
178 | renderGroup() { |
||
179 | const self = this; |
||
180 | |||
181 | // Handle dragging |
||
182 | const dragPoint = drag<SVGGElement, DataPointInternal>() |
||
183 | .on('drag', function (data) { |
||
184 | let { x } = event; |
||
185 | |||
186 | // Check point movement, preventing it from wondering outside the main curve |
||
187 | if (!x || x < 0) { |
||
188 | x = 0; |
||
189 | self.emit('home', { |
||
190 | ...data, |
||
191 | y: hillFnInverse(self.yScale.invert(data.y)), |
||
192 | }); |
||
193 | } else if (x > self.chartWidth) { |
||
194 | x = self.chartWidth; |
||
195 | self.emit('end', { |
||
196 | ...data, |
||
197 | x: self.xScale.invert(self.chartWidth), |
||
198 | y: hillFnInverse(self.yScale.invert(data.y)), |
||
199 | }); |
||
200 | } |
||
201 | |||
202 | // Convert current point coordinates back to the original |
||
203 | // between 0 and 100 to set it in the data attribute |
||
204 | const invertedX = self.xScale.invert(x); |
||
205 | |||
206 | data.x = x; |
||
207 | |||
208 | data.y = self.yScale(hillFn(invertedX)); |
||
209 | |||
210 | const invertedY = hillFnInverse(self.yScale.invert(data.y)); |
||
211 | |||
212 | const newInvertedCoordinates = { |
||
213 | x: invertedX, |
||
214 | y: invertedY, |
||
215 | }; |
||
216 | |||
217 | // click event |
||
218 | select<SVGGElement, DataPointInternal>(this).on('click', () => { |
||
219 | self.emit('pointClick', { ...data, ...newInvertedCoordinates }); |
||
220 | }); |
||
221 | |||
222 | if (!self.preview) { |
||
223 | const selectedPoint = select<SVGGElement, DataPointInternal>( |
||
224 | this |
||
225 | ).attr('transform', `translate(${data.x}, ${data.y})`); |
||
226 | selectedPoint |
||
227 | .select('text') |
||
228 | .style('text-anchor', () => { |
||
229 | if (textOutRange(invertedX)) { |
||
230 | return 'end'; |
||
231 | } |
||
232 | return 'start'; |
||
233 | }) |
||
234 | .attr('x', (point) => |
||
235 | calculateTextPositionForX(point.size, invertedX) |
||
236 | ); |
||
237 | |||
238 | self.emit('move', invertedX, invertedY); |
||
239 | } |
||
240 | }) |
||
241 | .on('end', (data) => { |
||
242 | if (this.preview) { |
||
243 | return; |
||
244 | } |
||
245 | |||
246 | let { x } = event; |
||
247 | |||
248 | // Check point movement, preventing it from wondering outside the main curve |
||
249 | if (!x || x < 0) { |
||
250 | x = 0; |
||
251 | } else if (x > this.chartWidth) { |
||
252 | x = this.chartWidth; |
||
253 | } |
||
254 | |||
255 | // Convert current point coordinates back to the original |
||
256 | const invertedX = this.xScale.invert(x); |
||
257 | data.y = this.yScale(hillFn(invertedX)); |
||
258 | const invertedY = hillFnInverse(this.yScale.invert(data.y)); |
||
259 | |||
260 | const newInvertedCoordinates = { |
||
261 | x: invertedX, |
||
262 | y: invertedY, |
||
263 | }; |
||
264 | |||
265 | this.emit('moved', { ...data, ...newInvertedCoordinates }); |
||
266 | }); |
||
267 | |||
268 | let group: |
||
269 | | Selection<SVGGElement, DataPointInternal, SVGGElement, unknown> |
||
270 | | undefined; |
||
271 | |||
272 | if (this.preview) { |
||
273 | group = this.undraggablePoint(); |
||
274 | } else { |
||
275 | // Create group consisted of a circle and a description text, where |
||
276 | // the data attributes determine the position of them on the curve |
||
277 | group = this.svg |
||
278 | .selectAll('.hill-chart-group') |
||
279 | .data(this.data) |
||
280 | .enter() |
||
281 | .append('g') |
||
282 | .attr('class', 'hill-chart-group') |
||
283 | .attr('transform', (data) => { |
||
284 | data.x = this.xScale(data.x); |
||
285 | data.y = this.yScale(data.y); |
||
286 | return `translate(${data.x}, ${data.y})`; |
||
287 | }) |
||
288 | .call(dragPoint); |
||
289 | } |
||
290 | |||
291 | group |
||
292 | .append('circle') |
||
293 | .attr('class', 'hill-chart-circle') |
||
294 | .attr('fill', (data) => data.color) |
||
295 | .attr('cx', 0) |
||
296 | .attr('cy', 0) |
||
297 | .attr('r', (data) => data.size || DEFAULT_SIZE); |
||
298 | |||
299 | group |
||
300 | .append('text') |
||
301 | .text((data) => data.description) |
||
302 | .attr('x', (data) => |
||
303 | calculateTextPositionForX( |
||
304 | data.size || DEFAULT_SIZE, |
||
305 | this.xScale.invert(data.x) |
||
306 | ) |
||
307 | ) |
||
308 | .style('text-anchor', (data) => { |
||
309 | if (textOutRange(this.xScale.invert(data.x))) { |
||
310 | return 'end'; |
||
311 | } |
||
312 | return 'start'; |
||
313 | }) |
||
314 | .attr('y', calculateTextMarginForY()); |
||
315 | } |
||
377 |